package daoImpl;

import dao.UserDao;
import util.MysqlConnectUtil;
import util.SQL;
import vo.Employee;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserDaoImpl implements UserDao {

    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;


    static{
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean addUser(Employee user) {

        boolean isSuccess = true;

        try{
            conn = MysqlConnectUtil.getConn();

            String sql_addUser = "insert into user values(?,?,?,?,?,?,?)";

            ps = conn.prepareStatement(sql_addUser);


            int addUserCount = ps.executeUpdate();

            if(addUserCount!=1){
                isSuccess = false;
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
           MysqlConnectUtil.closed(conn,ps);
        }

        return isSuccess;
    }

    @Override
    public Employee selectUserByUsername(String username) {


        Employee emp = null;

        try{

            conn = MysqlConnectUtil.getConn();

            String sql_selectUserByName = "select * from user where username=?";

            ps = conn.prepareStatement(sql_selectUserByName);

            ps.setString(1,username);

            ResultSet rs = ps.executeQuery();

            if(rs.next()){
                emp = new Employee();
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MysqlConnectUtil.closed(conn,ps,rs);
        }

        return emp;

    }

    @Override
    public Map<String,Object> selectEmpByUsernameAndPassword(String username, String password) {

        Map<String,Object> empMap = new HashMap<>();

        try{

            conn = MysqlConnectUtil.getConn();

            ps = conn.prepareStatement(SQL.sql_empLogin);

            ps.setString(1,username);
            ps.setString(2,password);

            ResultSet rs = ps.executeQuery();

            ResultSetMetaData metaData = rs.getMetaData();

            int columnCount = metaData.getColumnCount();

            if(rs.next()){

                for (int i = 1; i <= columnCount ; i++) {

                    String key = metaData.getColumnName(i);
                    Object value = rs.getObject(key);

                    empMap.put(key,value);
                }
            }


        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MysqlConnectUtil.closed(conn,ps,rs);
        }

        return empMap;
    }

    @Override
    public boolean modifyUser(Employee emp) {

        boolean isSuccess = true;

        try{

            conn = MysqlConnectUtil.getConn();

            String sql_updateUser = "update user set username=?,password=?,major=?,phoneNum=? where userId=? and isDelete=0";

            ps = conn.prepareStatement(sql_updateUser);


            int updateCount = ps.executeUpdate();

            if(updateCount != 1){
                isSuccess = false;
            }


        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MysqlConnectUtil.closed(conn,ps);
        }

        return isSuccess;
    }

    @Override
    public boolean deleteUser_reserve(String id) {

        boolean isSuccess = true;

        try{

            conn = MysqlConnectUtil.getConn();

            String sql_deleteUser = "update user set isDelete=1 where userId=? and isDelete=0";

            ps = conn.prepareStatement(sql_deleteUser);

            ps.setString(1,id);

            int deleteCount = ps.executeUpdate();

            if(deleteCount != 1){
                isSuccess = false;
            }


        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MysqlConnectUtil.closed(conn,ps);
        }

        return isSuccess;
    }

    @Override
    public boolean deleteUSer_notReserve(String id) {
        boolean isSuccess = true;

        try{

            conn = MysqlConnectUtil.getConn();

            String sql_deleteUser = "delete from user where userId=? and isDelete=0";

            ps = conn.prepareStatement(sql_deleteUser);

            ps.setString(1,id);

            int deleteCount = ps.executeUpdate();

            if(deleteCount != 1){
                isSuccess = false;
            }


        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MysqlConnectUtil.closed(conn,ps);
        }

        return isSuccess;
    }

    @Override
    public List<Employee> selectAllUser() {

        List<Employee> userList = new ArrayList<>();

        try{

            conn = MysqlConnectUtil.getConn();

            String sql_deleteUser = "select * from user where isDelete=0";

            ps = conn.prepareStatement(sql_deleteUser);

            rs = ps.executeQuery();

            while (rs.next()){
                Employee user = new Employee();
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MysqlConnectUtil.closed(conn,ps,rs);
        }

        return userList;
    }

    @Override
    public List<Employee> selectUserByCondition(String column, String value) {

        List<Employee> userList = new ArrayList<>();

        try{

            conn = MysqlConnectUtil.getConn();

            String sql_deleteUser = "select * from user where isDelete=0 and "+column+" =?";

            ps = conn.prepareStatement(sql_deleteUser);
            ps.setString(1,value);

            rs = ps.executeQuery();

            while (rs.next()){
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MysqlConnectUtil.closed(conn,ps,rs);
        }

        return userList;
    }
}
